package com.koron.css2.clientCenter.impl;

import com.koron.css2.ServerInterface;
import com.koron.css2.clientCenter.bean.*;
import com.koron.css2.clientCenter.mapper.ValveCloseDetailMapper;
import com.koron.css2.clientCenter.mapper.ValveCloseMapper;
import com.koron.css2.clientCenter.mapper.ValvePubMapper;
import com.koron.css2.clientCenter.mapper.ValveUserMapper;
import com.koron.css2.serviceManage.bean.RequestBean;
import com.koron.css2.serviceManage.bean.UserInfoBean;
import com.koron.css2.serviceManage.utils.JsonUtils;
import com.koron.util.Constant;
import com.koron.util.Tools;
import org.apache.commons.lang3.StringUtils;
import org.koron.ebs.mybatis.SessionFactory;
import org.springframework.beans.BeanUtils;
import org.swan.bean.MessageBean;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 关阀详情更新
 *
 * @author cd
 * @author modify by 杨嘉慧
 */
public class ValveCloseUpdate implements ServerInterface {
    @Override
    public MessageBean<?> exec(SessionFactory factory, UserInfoBean userInfo, RequestBean req) {

        try {
            ValveCloseAddBean addBean = JsonUtils.objectToPojo(req.getData(), ValveCloseAddBean.class);
            ValveCloseMapper valveCloseMapper = factory.getMapper(ValveCloseMapper.class);
            ValveCloseBean bean = valveCloseMapper.selectByPrimaryKey(addBean.getId());
            if (null != bean.getprocessState()) {
                return MessageBean.create(Constant.MESSAGE_INT_FAIL, "修改失败,该表单只有草稿状态下才能修改", null);
            }
            //更新关阀主表信息
            ValveCloseBean valveCloseBean = new ValveCloseBean();
            BeanUtils.copyProperties(addBean, valveCloseBean);
//            valveCloseBean.setPlanStartTime(Tools.getDate(addBean.getPlanStartTime()));
            valveCloseBean.setPlanTime(Tools.getDate(addBean.getPlanTime()));
            Date time = new Date();
            valveCloseBean.setUpdateName(userInfo.getUserInfo().getName());
            valveCloseBean.setUpdateTime(time);
            valveCloseMapper.updateByPrimaryKeySelective(valveCloseBean);
            //根据userNo添加关阀详情表信息
            List<ValveCloseDetailBean> userList = addBean.getUserNoList();

            ValveUserMapper valveUserMapper = factory.getMapper(ValveUserMapper.class);
            List<String> userNoList = userList.stream().map(ValveCloseDetailBean::getUserNo).collect(Collectors.toList());
            List<MeterReaderBean> meterReaderList = valveUserMapper.selectMeterReaderList(userNoList);
            Map<String, MeterReaderBean> meterReaderBeanMap = meterReaderList.stream().collect(Collectors.toMap(MeterReaderBean::getUserNo, meterReaderBean -> meterReaderBean, (value1, value2) -> {
                return value2;
            }));
            ValvePubMapper pubMapper = factory.getMapper(ValvePubMapper.class);
            List<ValveMessageBean> valveMessageBeanList = pubMapper.getValveMessage();
            Map<String, ValveMessageBean> valveMap = valveMessageBeanList.stream().collect(Collectors.toMap(ValveMessageBean::getId, meterReaderBean -> meterReaderBean));

            //user_info.meter_no获取用户的阀控类型 valveControlFlag (MS_INFO.meter_model   PUB_METER_MODEL.valve_control) 和  远控类型 remoteFlag MS_INFO.meter_type=2
            //组装关阀单明细数据
            List<ValveCloseDetailBean> detailBeans = new ArrayList<>();
            for (ValveCloseDetailBean user : userList) {
                MeterReaderBean meterReaderBean = meterReaderBeanMap.get(user.getUserNo());
                ValveCloseDetailBean detailBean = user;
                detailBean.setId(Tools.getObjectId());
                detailBean.setCreateTime(time);
                detailBean.setUserNo(user.getUserNo());
                detailBean.setValveCloseId(valveCloseBean.getId());
                detailBean.setCloseOverdueTime(valveCloseBean.getPlanTime());
                detailBean.setValveStatus("1");//关阀单明细数据初始化为待关阀
//                detailBean.setValveControlFlag(valveMap.get(meterReaderBean.getMeter_model()).getValveControlFlag());
                tobean(valveCloseBean, valveMap, detailBeans, meterReaderBean, detailBean);
            }

            ValveCloseDetailMapper detailMapper = factory.getMapper(ValveCloseDetailMapper.class);
            //原作者采用先删后加的方式进行明细数据的修改，可能存在问题
            detailMapper.delete(addBean.getId());
            detailMapper.insertBatch(detailBeans);

            return MessageBean.create(Constant.MESSAGE_INT_SUCCESS, "更新成功", null);
        } catch (Exception e) {
            logger.error("更新失败" + e.getMessage(), e);
            return MessageBean.create(Constant.MESSAGE_INT_FAIL, "更新失败", null);
        }
    }

    static void tobean(ValveCloseBean valveCloseBean, Map<String, ValveMessageBean> valveMap, List<ValveCloseDetailBean> detailBeans, MeterReaderBean meterReaderBean, ValveCloseDetailBean detailBean) {

        if (null != meterReaderBean.getMeter_model() && null != valveMap.get(meterReaderBean.getMeter_model()) && null != valveMap.get(meterReaderBean.getMeter_model()).getValveControlFlag()) {
            detailBean.setValveControlFlag(valveMap.get(meterReaderBean.getMeter_model()).getValveControlFlag());
        } else {
            detailBean.setValveControlFlag("0");
        }
        detailBean.setValveControlFlag(StringUtils.isBlank(meterReaderBean.getValveControlFlag()) ? "0" : meterReaderBean.getValveControlFlag());
        detailBean.setCancelFlag("0");
        detailBean.setCloseOverdueTime(valveCloseBean.getPlanTime());
        detailBean.setRemoteFlag(StringUtils.isBlank(meterReaderBean.getRemoteFlag()) ? "0" : meterReaderBean.getRemoteFlag());
        detailBean.setMeterReader(meterReaderBean.getMeterReader());
        detailBean.setMeterReaderAccount(meterReaderBean.getMeterReaderAccount());
        detailBean.setOperator(meterReaderBean.getMeterReader());
        detailBean.setOperatorAccount(meterReaderBean.getMeterReaderAccount());
        detailBeans.add(detailBean);
    }
}
